package com.sctvcloud.dongqu.ui.fragment;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Message;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.AbsoluteLayout;

import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.dialog.BaseDialogFragment;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.ui.dialog.NetModeDiaFragment;
import com.sctvcloud.dongqu.ui.utils.FloatGSYVideoUtils;
import com.sctvcloud.dongqu.ui.utils.OnFullClick;
import com.sctvcloud.dongqu.ui.utils.SampleListener;
import com.sctvcloud.dongqu.ui.widget.BasePlayerView;
import com.shuyu.gsyvideoplayer.listener.INetPromt;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import static com.sctvcloud.dongqu.ui.dialog.NetModeDiaFragment.NO_TAG;
import static com.sctvcloud.dongqu.ui.dialog.NetModeDiaFragment.OK_TAG;

/**
 * Created by Administrator on 2017/6/26 0026.
 * 首页 栏目的基础fragment
 */
public class ColumnBaseFragment extends BasePagerFragment {
    protected String channelUrl;
    protected int indexInViewPager = -1;
    protected NetModeDiaFragment netModeDiaFragment;
    public static final String NET_TAG = "dia_net_mode_fragment";
    protected String contentVideoUrl;
    protected View playView;
    protected FloatGSYVideoUtils floatVideoUtils;
    protected boolean isPortrait = true;
    protected static String subscribeUrl;
    protected String tag;
    private boolean isAutoRotation;

    protected boolean isAct = false;

    public void setAct(boolean isAct) {
        this.isAct = isAct;
    }

    protected OrientationUtils orientationUtils;

    public void setTag(String tag) {
        this.tag = tag;
    }

    protected SampleListener sampleListener = new SampleListener() {
        @Override
        public void onPrepared(String url, Object... objects) {
            super.onPrepared(url, objects);
//            if (internal == null) {
            if (floatVideoUtils != null) {
                floatVideoUtils.setPlayed(true);
            }
//            }
            if (orientationUtils != null) {
                orientationUtils.setEnable(true);
            }
        }

        @Override
        public void onClickResume(String url, Object... objects) {
            super.onClickResume(url, objects);
            if (orientationUtils != null) {
                orientationUtils.setEnable(true);
            }
        }

        @Override
        public void onClickResumeFullscreen(String url, Object... objects) {
            super.onClickResumeFullscreen(url, objects);
            if (orientationUtils != null) {
                orientationUtils.setEnable(true);
            }
        }
    };

    public static String getSubscribeUrl() {
        return subscribeUrl;
    }

    public static void setSubscribeUrl(String subscribeUrl) {
        ColumnBaseFragment.subscribeUrl = subscribeUrl;
    }

    public void setChannelUrl(String channelUrl) {
//        JLog.e("ttst  will set channel url " + this + "\n  " + channelUrl);
        this.channelUrl = channelUrl;
    }

    public String getChannelUrl() {
        return channelUrl;
    }

    public int getIndexInViewPager() {
        return indexInViewPager;
    }

    public void setIndexInViewPager(int indexInViewPager) {
        this.indexInViewPager = indexInViewPager;
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        isPortrait = newConfig.orientation == Configuration.ORIENTATION_PORTRAIT;
        if (isPortrait) {
            onChangedToPortrait(newConfig);
        } else {
            onChangedToLandscape(newConfig);
        }
        JLog.e("qqwq  float=" + floatVideoUtils + "  isFront=" + isFront + "   " + this);
        if (floatVideoUtils != null && isFront) {
            floatVideoUtils.onConfigurationChanged(getActivity(), newConfig, orientationUtils);
        }
    }

    protected int getCurrentScrollY() {
        return 0;
    }

    protected void onChangedToPortrait(Configuration newConfig) {
    }

    protected void onChangedToLandscape(Configuration newConfig) {
    }

    @Override
    public void onResume() {
        super.onResume();
//        if (floatVideoUtils!=null){
//            floatVideoUtils.onResume();
//        }
    }

    @Override
    public void onPause() {
//        if (floatVideoUtils!=null){
//            floatVideoUtils.onPause();
//        }
        super.onPause();
    }

    @Override
    protected boolean onFragmentShow(boolean isFirst, boolean isCallFromResume) {
        isFront = true;
        if (!isFirst) {
            if (floatVideoUtils != null && floatVideoUtils.isShowing()) {
                JLog.e("vvav  will onResume  +" + this);
                floatVideoUtils.onResume();
                if (orientationUtils != null) {
                    orientationUtils.setEnable(true);
                }
            }
        }
        return super.onFragmentShow(isFirst, isCallFromResume);
    }

    @Override
    protected void onFragmentUnShowed(boolean isCallFromPause) {
        super.onFragmentUnShowed(isCallFromPause);
        if (orientationUtils != null) {
            orientationUtils.setEnable(false);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (floatVideoUtils != null) {
            floatVideoUtils.onDestory();
        }
    }

    protected boolean initFloat(View parentView, AbsoluteLayout videoLayout, int otherHeight) {
        boolean b = floatVideoUtils == null;
        if (b) {
            floatVideoUtils = new FloatGSYVideoUtils(getContext(), parentView, videoLayout, otherHeight);
            floatVideoUtils.setPlayerFactor(new FloatGSYVideoUtils.IPlayerFactor() {
                @Override
                public BasePlayerView createPlayerView() {
                    BasePlayerView bpv = createdPlayerView();
                    initVideoPlayer(bpv);
                    return bpv;
                }
            });
        }
        return b;
    }

    protected void initVideoPlayer(@NonNull BasePlayerView player) {
//        player.setStandardVideoAllCallBack();
        player.setFullClick(new OnFullClick() {
            @Override
            public void onFullClick(StandardGSYVideoPlayer player, boolean isLandscape) {
                JLog.e("vvav  full click isLandScape=" + isLandscape + "   player=" + player);
                onFullClicked(player, isLandscape);
            }
        });
        if (orientationUtils == null) {
//            orientationUtils = new OrientationUtils(getActivity(), player);
//            orientationUtils.setRotateWithSystem(false);
//            orientationUtils.setEnable(false);
        }
        if (floatVideoUtils != null) {
            floatVideoUtils.setSampleListener(sampleListener);
        } else {
            player.setStandardVideoAllCallBack(sampleListener);
        }
        player.setNetPromt(new INetPromt() {
            @Override
            public boolean onNetPromt() {
                showNetDialog();
                return false;
            }
        });
    }

    @NonNull
    protected BasePlayerView createdPlayerView() {
        return new BasePlayerView(getContext());
    }

    protected boolean isDataGetting() {
        return false;
    }

    protected void onFullClicked(StandardGSYVideoPlayer player, boolean isLandscape) {
        if (player == null || TextUtils.isEmpty(player.getOriUrl())) {
            if (isDataGetting()) {
                toast(R.string.loading_data_wait);
            } else {
                toast(R.string.data_wrong);
            }
            return;
        }
        if (isLandscape) {
            if (orientationUtils != null) {
                orientationUtils.backToProtVideo();
            } else {
                actionToPortrait();
            }
            StandardGSYVideoPlayer.backFromWindowFull(getContext());
        } else {
            if (orientationUtils != null) {
                orientationUtils.resolveByClick();
            } else {
                JLog.e("vvav   will to Landscape");
                //判断视频源是横屏还是竖屏的，弱势竖屏的，就不转向
                if (player.getWidthToHeightRatio() == 0 || player.getWidthToHeightRatio() > 1) {
                    //第一个true是否需要隐藏actionbar，第二个true是否需要隐藏statusbar
                    actionToLandscape();//在这切换为横屏
                }
            }

            //第一个true是否需要隐藏actionbar，第二个true是否需要隐藏statusbar
            player = (BasePlayerView) player.startWindowFullscreen(getContext(), true, true);
            if (player != null) {
                player.getBackButton().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        JLog.e("vvav  will back press");
                        onBackPressed();
                    }
                });
            }
        }
    }

    protected void actionToPortrait() {
        if (!isPortrait) {
            if (isAutoRotation) {
                getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
            } else {
                getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
        }
    }

    protected void actionToLandscape() {
        if (isPortrait) {
            if (isAutoRotation) {
                getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
            } else {
                getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }
        }
    }

    protected void showNetDialog() {
        if (netModeDiaFragment == null) {
            netModeDiaFragment = new NetModeDiaFragment();
        }
        if(!netModeDiaFragment.isAdded()) {
            netModeDiaFragment.setCallBack(netCallBack);
            netModeDiaFragment.show(getFragmentManager(), NET_TAG);
        }
    }

    protected void onNetCallBack(boolean isContinue) {
        if (floatVideoUtils != null) {
            if (isContinue) {
                floatVideoUtils.getPlayerView().startPlayLogic();
            }
        }
    }


    BaseDialogFragment.ICallBack netCallBack = new BaseDialogFragment.ICallBack() {
        @Override
        public void onCallBackMsg(BaseDialogFragment fragment, Message msg) {
            if (msg.what == NO_TAG) {
//                floatVideoUtils.stopVideoView();
                onNetCallBack(false);
            } else if (msg.what == OK_TAG) {
//                floatVideoUtils.setLiving(false).play(playView, contentVideoUrl);
                onNetCallBack(true);
            }
        }
    };

    public synchronized void setSampleListener(SampleListener sampleListener) {
        if (sampleListener == null) {
            return;
        }
        sampleListener.setInternal(this.sampleListener);
        if (floatVideoUtils != null) {
            floatVideoUtils.setSampleListener(sampleListener);
        } else {
//            sampleListener.setInternal(this.sampleListener);
//            floatVideoUtils.setSampleListener(sampleListener);
            this.sampleListener = sampleListener;
        }
    }
}
